/*
 * Copyright (C) 2017 Pingan, Inc. All Rights Reserved.
 */

package com.pingan.haofang.searchcloud.index.service.impl;

import com.pingan.haofang.searchcloud.index.dto.IndexBuildProgressDTO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.connection.DefaultStringRedisConnection;
import org.springframework.data.redis.connection.RedisConnection;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.RedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;
import org.springframework.stereotype.Service;


/**
 * 索引构建进度redisTemplate
 *
 * @author zhangbi617
 * @date 15/05/2017
 */
@Service
public class IndexBuildProgressRedisTemplate extends RedisTemplate<String, IndexBuildProgressDTO> {

    public IndexBuildProgressRedisTemplate() {
        RedisSerializer<String> stringSerializer = new StringRedisSerializer();
        Jackson2JsonRedisSerializer<IndexBuildProgressDTO> indexBuildProgressDTOJackson2JsonRedisSerializer = new
                Jackson2JsonRedisSerializer<>(IndexBuildProgressDTO.class);
        setKeySerializer(stringSerializer);
        setValueSerializer(indexBuildProgressDTOJackson2JsonRedisSerializer);
        setHashKeySerializer(stringSerializer);
        setHashValueSerializer(indexBuildProgressDTOJackson2JsonRedisSerializer);
    }

    @Autowired
    public IndexBuildProgressRedisTemplate(RedisConnectionFactory connectionFactory) {
        this();
        setConnectionFactory(connectionFactory);
        afterPropertiesSet();
    }

    protected RedisConnection preProcessConnection(RedisConnection connection, boolean existingConnection) {
        return new DefaultStringRedisConnection(connection);
    }
}
